package bearworker.zxl.utils;

import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by 逍遥的熊 on 2017/12/3.
 */
public class RedisUtil {

    private static StringRedisTemplate redisTemplate = (StringRedisTemplate) SpringContext.getBean("redisTemplate");

    private static Gson gson = new Gson();

    private static Logger logger = Logger.getLogger(RedisUtil.class);

    public static void set(String key,String value){
        logger.debug(key);
        logger.debug(value);
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        valueOperations.set(key,value);
    }

    public static Map<String,String> get(String key){
        logger.debug(key);
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        Map<String,String> reMap = new HashMap<>();

        reMap.put(key,valueOperations.get(key));

        return reMap;

    }

    public static void setExp(String key,Long timeout){
        redisTemplate.expire(key,timeout, TimeUnit.SECONDS);
    }

}
